<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        ref="table"
        :btnList="btnList"
        :total="totalNum"
        v-model="searchParams"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :tableColumn="tableColumn"
        requestName="searchHandle"
        :tableData="tableData"
        @edit="operateHandle($event, 'edit')"
        @view="operateHandle($event, 'view')"
        @export="operateHandle($event, 'export')"
      />
    </div>

    <TrEditPopup
      ref="TrEditPopup"
      :width="460"
      :labelWidth="114"
      v-model="isShowEdit"
      :title="checkTitle"
      :valueObj.sync="formObj"
      :formData="formData"
      :list="popupList"
      @close="isShowEdit = false"
      @confrim="editConfirmHandle"
      @radio-change="changeRadioHandle"
    />

    <TrDetail
      v-model="isShowDetailPopup"
      popupTitle="仓库维护"
      :leftList="detailList"
      statusModel="hidden"
      :labelWidth="112"
      :detailData="detailData"
      :isNeedFooter="false"
      @close="closeDetailPopup"
    />
  </div>
</template>

<script>
import { getList, update } from "@/api/baseCode/warehouseMaintenance";
import { findByProperty } from "@/utils/util";
import { getColor } from "@/utils/billColor";
import enumData from "@/utils/enumCode.js";

import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrEditPopup from "@/components/Tr-edit/Tr-edit-popup.vue";
import TrDetail from "@/components/Tr-detail/Tr-detail.vue";

export default {
  components: {
    TrTable,
    TrEditPopup,
    TrDetail,
  },
  name: "warehouseMaintenance",
  data: () => ({
    tableColumn: [
      {
        type: "text",
        label: "仓库编码",
        value: "warehouseCode",
        fixed: "left",
      },
      {
        type: "text",
        label: "仓库名称",
        value: "warehouseName",
      },
      {
        type: "text",
        label: "仓库描述",
        value: "remark",
      },
      {
        type: "text",
        label: "仓库属性",
        value: "warehouseAttrName",
      },
      {
        type: "text",
        label: "启用交易上传",
        value: "isDealUploadName",
      },
      {
        type: "text",
        label: "启用交易上传时间",
        value: "dealUploadTime",
      },
      {
        type: "text",
        label: "仓库状况",
        value: "isEnabledName",
      },
      {
        type: "text",
        label: "创建人员",
        value: "createUserName",
      },
      {
        type: "text",
        label: "创建时间",
        value: "createTime",
      },
      {
        type: "text",
        label: "修改人员",
        value: "updateUserName",
      },
      {
        type: "text",
        label: "修改时间",
        value: "updateTime",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        width: 120,
        showBtnList: [
          {
            label: "查看",
            option: "view",
          },
          {
            label: "编辑",
            option: "edit",
          },
        ],
      },
    ],
    searchHideList: [
      {
        id: "warehouseCode",
        type: "input",
        width: 33.33,
        label: "仓库编码",
        value: "",
      },
      {
        id: "warehouseName",
        type: "input",
        width: 33.33,
        label: "仓库名称",
        value: "",
      },
      {
        id: "warehouseAttr",
        type: "select",
        width: 33.33,
        label: "仓库属性",
        value: "",
      },
      {
        id: "isEnabledList",
        type: "multiSelect",
        list: [],
        width: 33.33,
        label: "仓库状况",
        value: [],
      },
      {
        startStr: "updateTimeStart",
        endStr: "updateTimeEnd",
        id: "modifyTime",
        type: "daterange",
        width: 80,
        label: "修改时间",
        value: [],
        timePeriod: true,
        radio: -1,
      },
    ],
    searchShowList: [
      {
        isNeed: true,
        startStr: "createTimeStart",
        endStr: "createTimeEnd",
        id: "createdTime",
        type: "daterange",
        width: 80,
        label: "创建时间",
        value: [],
        timePeriod: true,
        radio: 1,
      },
    ],
    btnList: [
      {
        label: "导出",
        option: "export",
        color: "blue",
      },
    ],
    tableData: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,

    detailData: {},
    isShowDetailPopup: false,
    detailList: [
      {
        type: "text",
        label: "基础信息",
        list: [
          {
            text: "仓库编码",
            id: "warehouseCode",
            width: 50,
          },
          {
            text: "仓库名称",
            id: "warehouseName",
            width: 50,
          },
          {
            text: "仓库属性",
            id: "warehouseAttrName",
            width: 50,
          },
          {
            text: "仓库状况",
            id: "isEnabledName",
            width: 50,
          },
          {
            text: "启用交易上传",
            id: "isDealUploadName",
            width: 50,
          },
          {
            text: "启用交易上传时间",
            id: "dealUploadTime",
            width: 50,
          },
          {
            text: "仓库描述",
            id: "remark",
            width: 100,
          },
        ],
      },
    ],

    // 新增编辑弹窗
    formObj: {},
    formData: {},
    popupList: [
      {
        label: "仓库编码",
        type: "input",
        width: 100,
        value: "",
        id: "warehouseCode",
        disabled: true,
      },
      {
        label: "仓库名称",
        type: "input",
        width: 100,
        value: "",
        id: "warehouseName",
        disabled: true,
      },
      {
        label: "仓库属性",
        type: "select",
        width: 100,
        value: "",
        id: "warehouseAttr",
        list: [],
        isNeed: false,
      },
      {
        label: "是否启用仓库",
        type: "radio",
        width: 100,
        value: 0,
        id: "isEnabled",
        list: [
          { label: "是", value: 1 },
          { label: "否", value: 0 },
        ],
      },
      {
        label: "是否启用交易上传",
        type: "radio",
        width: 100,
        value: 0,
        id: "isDealUpload",
        list: [
          { label: "是", value: 1 },
          { label: "否", value: 0 },
        ],
      },
      {
        label: "启用交易上传时间",
        type: "datetime",
        width: 100,
        value: "",
        id: "dealUploadTime",
      },
      {
        isNeed: false,
        label: "仓库描述",
        type: "textarea",
        width: 100,
        value: "",
        id: "remark",
      },
    ],
    isShowEdit: false,
    currentHandle: "",
    currentRowVal: {},
  }),
  computed: {
    checkTitle() {
      const titles = {
        add: "新增",
        default: "编辑",
      };
      return titles[this.currentHandle] || titles.default;
    },
    ids() {
      let ids = [];
      this.$refs.table.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
  mounted() {
    // this.getListHandle();
    this.getEnmuData();
  },
  methods: {
    async getEnmuData() {
      const { ["PC-ENABLED-LIST"]: enabled } = await this.$fetchEnumData([
        enumData.isEnabledList,
      ]);
      const { ["PC-WAREHOUSE-ATTR"]: warehouseAttr } =
        await this.$fetchEnumData([enumData.warehouseAttr]);

      const enabledItem = findByProperty(
        this.searchHideList,
        "id",
        "isEnabledList"
      );

      const warehouseAttrItem = findByProperty(
        this.searchHideList,
        "id",
        "warehouseAttr"
      );

      const warehouseAttrPopupItem = findByProperty(
        this.popupList,
        "id",
        "warehouseAttr"
      );

      warehouseAttrItem.list = warehouseAttr;

      warehouseAttrPopupItem.list = warehouseAttr;

      enabledItem.list = [
        ...enabled.slice(0, 2).map((item) => ({ ...item, checked: false })),
      ];
    },
    async operateHandle(val, type) {
      this.currentHandle = type;

      this.$validateData(val, 3);

      const actions = {
        edit: async () => {
          this.currentRowVal = val;

          this.formData = val;

          const dealUploadTime = findByProperty(
            this.popupList,
            "id",
            "dealUploadTime"
          );

          this.$set(dealUploadTime, "isNeed", !val.isDealUpload == 0);
          this.formData.dealUploadTime =
            val.isDealUpload == 0 ? "" : val.dealUploadTime;

          this.isShowEdit = true;
        },
        view: () => this.getDetailHandle(val),
        export: () =>
          this.$exportHandle({ vm: this, params: this.searchParams }),
      };

      const action = actions[type];
      if (action) action();
    },

    async getDetailHandle(val) {
      // const { data } = await getDetail(val.id);

      // if (data.code !== 200) return;
      const col = this.$findByProperty(
        this.detailList,
        "id",
        "isEnabledName",
        "list"
      );
      col.valueColor = getColor("QT", val.isEnabled);
      const dealCol = this.$findByProperty(
        this.detailList,
        "id",
        "isDealUploadName",
        "list"
      );
      dealCol.valueColor = getColor("QT", val.isDealUpload);
      this.$validateData(val);

      this.detailData = val;

      this.isShowDetailPopup = true;
    },
    closeDetailPopup() {
      this.isShowDetailPopup = false;
    },

    searchHandle() {
      this.getListHandle();
    },
    changeRadioHandle(val) {
      if (val.id === "isDealUpload") {
        const cloneList = this.$refs.TrEditPopup?.$refs.trEdit?.cloneList;
        const dealUploadTime = findByProperty(
          cloneList,
          "id",
          "dealUploadTime"
        );

        this.$set(dealUploadTime, "isNeed", !val.value == 0);
        this.$set(
          dealUploadTime,
          "value",
          val.value == 0 ? "" : this.currentRowVal.dealUploadTime
        );
      }
    },
    async getListHandle() {
      const { data } = await getList(this.searchParams);
      if (data.code != 200) return;
      this.$validateData(data.data);
      this.tableData = data.data.records;
      this.totalNum = Number(data.data.total);

      this.tableData.forEach((item) => {
        item["isEnabledNameColor"] = getColor("QT", item.isEnabled);
        item["isDealUploadNameColor"] = getColor("QT", item.isDealUpload);
      });
    },
    async editHandle() {
      const { data } = await update({
        ...this.formObj,
        id: this.currentRowVal.id,
      });
      return data.code === 200;
    },
    async editConfirmHandle() {
      let flag = false;

      flag = await this.editHandle();

      if (!flag) return;
      this.$message.success(this.$t("msg.success"));
      this.isShowEdit = false;
      this.searchParams.current = 1;
      this.getListHandle();
    },
  },
};
</script>
